<?php
/* 
 * Object sach
 */
class Sach extends DataMapper
{
    var $validation = array(
        array(
            'field' => 'tensach',
            'rules' => array('required')
        ),
        array(
            'field' => 'soluong',
            'rules' => array('required')
        )
    );


    public function Sach()
    {
        parent::DataMapper();
    }
    // get_book_radom()
    public function  get_book_random($book)
    {
        $s = new Sach();
        $s->where("loaisach", $book)->get();
        $id = array(); $ten = array(); $soluong = array(); $gia = array(); $giaban = array(); $tacgia = array();
        $nxb = array(); $namxb = array(); $loaisach = array(); $ngaynhap = array();
        foreach($s->all as $ts)
        {
            $id[] = $ts->id;
            $ten[] = $ts->tensach;
            $soluong[] = $ts->soluong;
            $gia[] = $ts->gia;
            $giaban[] = $ts->giaban;
            $tacgia[] = $ts->tacgia;
            $nxb[] = $ts->nxb;
            $namxb[] = $ts->namxb;
            $loaisach[] = $ts->loaisach;
            $ngaynhap[] = $ts->ngaynhap;
        }
        $data = array('id'=>$id, 'ten'=>$ten, 'soluong'=>$soluong, 'gia'=>$gia, 'giaban'=>$giaban, 'tacgia'=>$tacgia,
                    'nxb'=>$nxb, 'namxb'=>$namxb, 'loaisach'=>$loaisach, 'ngaynhap'=>$ngaynhap);
        // tra ve mang cac ket qua
        return $data;
    }// get_book_random()
    

    // get_by_id()
    public function get_by_id_sach($id)
    {
        $s = new Sach();
        $s->where("id", $id)->get();
        foreach($s->all as $ts)
        {
            $data = array('id'=>$ts->id, 'ten'=>$ts->tensach, 'soluong'=>$ts->soluong, 'gia'=>$ts->gia, 'giaban'=>$ts->giaban,
                'tacgia'=>$ts->tacgia, 'nxb'=>$ts->nxb, 'namxb'=>$ts->namxb, 'loaisach'=>$ts->loaisach, 'ngaynhap'=>$ts->ngaynhap);
        }
        // neu ket qua khong tim thay - auto chuyen nguoi dung den trang can den
        if(count($data['id'])==0)
        {
            return redirect("home");
        }
        else// neu tim thay ket qua thi hien thi
        {
            return $data;
        }
    }// End function get_by_id()

        
    // get_number_cmt()
    public function get_number_cmt($id)
    {
        $s = new Sach();
        $s->where("id", $id)->get();
        $s->comment->get_iterated();
        $total_cmt = array();
        foreach($s->comment as $c)
        {
            $total_cmt[] = $c->id;
        }
        return count($total_cmt);
    }

    // update total comment
    public function update_total_cmt($id)
    {
        $s = new Sach();
        $total = $s->get_number_cmt($id);
        $s->where('id', $id);
        return $s->update('totalcmt', $total);
    }// End update total comment

    // update status total comment
    public function update_status_cmt($id)
    {
        $s = new Sach();
        $total = $s->get_number_cmt($id);
        $s->where('id', $id);
        $flag = 0;
        if($total>=5)
        {
            $flag = 1;
        }
        return $s->update('status', $flag);
    }// End update status


    // get_sach_quantam()
    public function get_sach_quantam()
    {
        $s = new Sach();
        // nhung sach co status = 1
        $s->where("status", 1)->get();
        $total = array(); $ten = array(); $tacgia = array(); $theloai = array(); $nxb = array();
        $namxb = array(); $mota = array();
        foreach($s->all as $ts)
        {
            $total[] = $ts->id;
            $ten[] = $ts->tensach;
            $tacgia[] = $ts->tacgia;
            $nxb[] = $ts->nxb;
            $theloai[] = $ts->loaisach;
            $namxb[] = $ts->namxb;
            $mota[] = $ts->mota;
        }
        $data = array('total'=>$total, 'ten'=>$ten, 'tacgia'=>$tacgia, 'nxb'=>$nxb, 'theloai'=>$theloai,
                'namxb'=>$namxb, 'mota'=>$mota);
        return $data;
    }// End get_sach_quantam()

    
    /* san pham cu nhat con ton tai trong thang
     * = tong cac san pham con ton tai trong tuan dau tien cua thang
     */
    public function get_product_inweek()
    {
        $s = new Sach();
        $option = 'd';
        $object = date('Y-m-d');
        $object2 = "ngaynhap";

        $s->where(date_diff($object, $object2, $option), 5  );
        $total = array(); $ten = array(); $tacgia = array(); $theloai = array(); $namxb = array();
        foreach($s->get()->all as $t)
        {
            $total[] = $t->id;
            $ten[] = $t->tensach;
            $namxb[] = $t->namxb;
        }
        $data = array("id"=>$total, "ten"=>$ten, "namxb"=>$namxb);
        return $data;
    }

    /* Tong cac san pham cu nhat con ton tai trong quy
     * = tong cac san pham cu nhat con ton tai o moi thang cua quy do
     */

    /*
    *  add_book()
    */
    public function add_book()
    {
        $s = new Sach();        
    }

}

